package gameObjects;

import java.io.IOException;
import java.net.URL;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;

public class Object3D extends Node{
	private static final long serialVersionUID = 1904101611L;
	protected Node model3D;
	protected TextureMapping texture = new TextureMapping();
	protected DisplaySystem  display = null;
	
	public Object3D()
	{
		super("OBJECT3D");
		this.display=DisplaySystem.getDisplaySystem();
		model3D = new Node("MODEL3D");
		this.attachChild(model3D);
	}
	
	public Object3D(String name)
	{
		super(name);
		this.display=DisplaySystem.getDisplaySystem();
		model3D = new Node(name);
		this.attachChild(model3D);	
	}
	
	public Spatial getModel3D()
	{
		return this.model3D;
	}
	
	public Spatial getChildByName(String name)
	{
		return model3D.getChild(name);
	}
	
	public Spatial getChildById(int id)
	{
		return model3D.getChild(id);
	}
	
	public void addNewChild(Spatial child)
	{
		model3D.attachChild(child);
	}
	
	public Spatial delChild(int id)
	{
		Spatial node = model3D.getChild(id);
		model3D.detachChildAt(id);
		return node;
	}
	
	public Spatial delChild(String name)
	{
		Spatial node = model3D.getChild(name);
		model3D.detachChildNamed(name);
		return node;
	}
	
	public void clearChildren()
	{
		model3D.detachAllChildren();
	}
	
	public Spatial getModel(String path) throws IOException
	{
		System.out.println("getModel " + path);
		URL url = Object3D.class.getClassLoader().getResource(path);
		BinaryImporter importer = new BinaryImporter();
		Spatial model = (Spatial) importer.load(url);
		model.setName(path);
		model.setCullHint(CullHint.Dynamic);
		this.model3D = (Node) model;
		this.detachAllChildren();
		this.attachChild(model3D);
		return model;
	}
}
